import {createRouter, createWebHistory} from 'vue-router'
import {useStore} from "@/stores/userStore";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL), routes: [{
        path: '/',
        name: 'welcome',
        component: () => import('@/views/WelcomeView.vue'),
        children: [{
            path: '',
            name: 'welcome-login',
            component: () => import('@/components/welcome/LoginPage.vue')
        }, {
            path: 'register',
            name: 'welcome-register',
            component: () => import('@/components/welcome/RegisterPage.vue')
        }, {
            path: 'forget',
            name: 'welcome-forget',
            component: () => import('@/components/welcome/ForgetPage.vue')
        }]
    }, {
        path: '/index',
        name: 'index',
        component: () => import('@/views/IndexView.vue'),
        children: [{
            path: '',
            name: 'index-home',
            component: () => import('@/components/index/HomePage.vue')
        }, {
            path: '/index/user',
            name: 'index-user',
            component: () => import('@/components/index/UserPage.vue')
        }, {
            path: '/index/singer',
            name: 'index-singer',
            component: () => import('@/components/index/SingerPage.vue')
        }, {
            path: '/index/add-singer',
            name: 'index-addSinger',
            component: () => import('@/components/index/NewSingerPage.vue')
        }, {
            path: '/index/update-singer',
            name: 'index-updateSinger',
            component: () => import('@/components/index/UpdateSingerPage.vue')
        }, {
            path: '/index/game',
            name: 'index-game',
            component: () => import('@/components/index/GamePage.vue')
        }, {
            path: '/index/add-game',
            name: 'index-addGame',
            component: () => import('@/components/index/NewGamePage.vue')
        }, {
            path: '/index/update-game',
            name: 'index-updateGame',
            component: () => import('@/components/index/UpdateGamePage.vue')
        }, {
            path: '/index/statistic-game',
            name: 'index-staticGame',
            component: () => import('@/components/index/GameStatistic.vue')
        }, {
            path: '/index/vote',
            name: 'index-vote',
            component: () => import('@/components/index/VotePage.vue')
        }, {
            path: '/index/judge-score',
            name: 'index-judgeScore',
            component: () => import('@/components/index/JudgeScorePage.vue')
        }]
    }]
})

router.beforeEach((to, from, next) => {
    const store = useStore()
    const isAuthenticated = store.auth.user !== null;
    if (isAuthenticated) {
        // 用户已登录
        if (to.name.startsWith('welcome-')) {
            next('/index');
        } else {
            next();
        }
    } else {
        // 用户未登录
        if (to.fullPath.startsWith('/index')) {
            next('/');
        } else if (to.matched.length === 0) {
            next('/index');
        } else {
            next();
        }
    }
})

export default router
